package com.ycuwq.datepicker.date;

import com.ycuwq.datepicker.WheelPicker;
import ohos.agp.components.AttrSet;
import ohos.app.Context;

import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * 月份选择器
 * Created by ycuwq on 17-12-28.
 */
public class MonthPicker extends WheelPicker<Integer> {
    /**
     * * The constant MAX_MONTH
     */
    private static final int MAX_MONTH = 12;
    /**
     * * The constant MIN_MONTH
     */
    private static final int MIN_MONTH = 1;

    /**
     * The constant M selected month
     */
    private int mSelectedMonth;

    /**
     * The constant M on month selected listener
     */
    private OnMonthSelectedListener mOnMonthSelectedListener;

    /**
     * The constant M year
     */
    private int mYear;
    /**
     * The constant M max date
     */
    private long mMaxDate;
    /**
     * M min date
     */
    private long mMinDate;
    /**
     * The constant M max year
     */
    private int mMaxYear;
    /**
     * M min year
     */
    private int mMinYear;
    /**
     * The constant M min month
     */
    private int mMinMonth = MIN_MONTH;
    /**
     * The constant M max month
     */
    private int mMaxMonth = MAX_MONTH;

    /**
     * Month picker
     *
     * @param context context
     */
    public MonthPicker(Context context) {
        this(context, null);
    }

    /**
     * Month picker
     *
     * @param context context
     * @param attrs   attrs
     */
    public MonthPicker(Context context, AttrSet attrs) {
        this(context, attrs, null);
    }

    /**
     * Month picker
     *
     * @param context      context
     * @param attrs        attrs
     * @param defStyleAttr def style attr
     */
    public MonthPicker(Context context, AttrSet attrs, String defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setItemMaximumWidthText("00");
        NumberFormat numberFormat = NumberFormat.getNumberInstance();
        numberFormat.setMinimumIntegerDigits(2);
        setDataFormat(numberFormat);

        Calendar.getInstance().clear();
        mSelectedMonth = Calendar.getInstance().get(Calendar.MONTH) + 1;
        updateMonth();
        setSelectedMonth(mSelectedMonth, false);
        setOnWheelChangeListener(new OnWheelChangeListener<Integer>() {
            @Override
            public void onWheelSelected(Integer item, int position) {
                mSelectedMonth = item;
                if (mOnMonthSelectedListener != null) {
                    mOnMonthSelectedListener.onMonthSelected(item);
                }
            }
        });
    }

    /**
     * Update month
     */
    public void updateMonth() {
        List<Integer> list = new ArrayList<>();
        for (int i = mMinMonth; i <= mMaxMonth; i++) {
            list.add(i);
        }
        setDataList(list);
    }

    /**
     * Set max date *
     *
     * @param date date
     */
    public void setMaxDate(long date) {
        mMaxDate = date;
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(date);
        mMaxYear = calendar.get(Calendar.YEAR);
    }

    /**
     * Set min date *
     *
     * @param date date
     */
    public void setMinDate(long date) {
        mMinDate = date;
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(date);
        mMinYear = calendar.get(Calendar.YEAR);
    }


    /**
     * Set year *
     *
     * @param year year
     */
    public void setYear(int year) {
        mYear = year;
        mMinMonth = MIN_MONTH;
        mMaxMonth = MAX_MONTH;
        if (mMaxDate != 0 && mMaxYear == year) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(mMaxDate);
            mMaxMonth = calendar.get(Calendar.MONTH) + 1;
        }
        if (mMinDate != 0 && mMinYear == year) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(mMinDate);
            mMinMonth = calendar.get(Calendar.MONTH) + 1;
        }
        updateMonth();
        if (mSelectedMonth > mMaxMonth) {
            setSelectedMonth(mMaxMonth, false);
        } else if (mSelectedMonth < mMinMonth) {
            setSelectedMonth(mMinMonth, false);
        } else {
            setSelectedMonth(mSelectedMonth, false);
        }
    }

    /**
     * Get selected month int
     *
     * @return the int
     */
    public int getSelectedMonth() {
        return mSelectedMonth;
    }

    /**
     * Set selected month *
     *
     * @param selectedMonth selected month
     */
    public void setSelectedMonth(int selectedMonth) {
        setSelectedMonth(selectedMonth, true);
    }

    /**
     * Set selected month *
     *
     * @param selectedMonth selected month
     * @param smoothScroll  smooth scroll
     */
    public void setSelectedMonth(int selectedMonth, boolean smoothScroll) {
        setCurrentPosition(selectedMonth - mMinMonth, smoothScroll);
        mSelectedMonth = selectedMonth;
    }

    /**
     * Set on month selected listener *
     *
     * @param onMonthSelectedListener on month selected listener
     */
    public void setOnMonthSelectedListener(OnMonthSelectedListener onMonthSelectedListener) {
        mOnMonthSelectedListener = onMonthSelectedListener;
    }

    /**
     * On month selected listener
     */
    public interface OnMonthSelectedListener {
        /**
         * On month selected *
         *
         * @param month month
         */
        void onMonthSelected(int month);
    }

}
